package com.ruoyi.zsyh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.zsyh.domain.ZsyhPersonOrderMoney;
import com.ruoyi.zsyh.mapper.ZsyhPersonOrderMapper;
import com.ruoyi.zsyh.domain.ZsyhPersonOrder;
import com.ruoyi.zsyh.service.IZsyhPersonOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 个人扫码单Service业务层处理
 * 
 * @author yuexl
 * @date 2024-08-22
 */
@Service
public class ZsyhPersonOrderServiceImpl implements IZsyhPersonOrderService 
{
    @Autowired
    private ZsyhPersonOrderMapper zsyhPersonOrderMapper;

    /**
     * 查询个人扫码单
     * 
     * @param id 个人扫码单ID
     * @return 个人扫码单
     */
    @Override
    public ZsyhPersonOrder selectZsyhPersonOrderById(String id)
    {
        return zsyhPersonOrderMapper.selectZsyhPersonOrderById(id);
    }

    /**
     * 查询个人扫码单列表
     * 
     * @param zsyhPersonOrder 个人扫码单
     * @return 个人扫码单
     */
    @Override
    public List<ZsyhPersonOrder> selectZsyhPersonOrderList(ZsyhPersonOrder zsyhPersonOrder)
    {
        return zsyhPersonOrderMapper.selectZsyhPersonOrderList(zsyhPersonOrder);
    }

    /**
     * 新增个人扫码单
     * 
     * @param zsyhPersonOrder 个人扫码单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZsyhPersonOrder(ZsyhPersonOrder zsyhPersonOrder)
    {
        zsyhPersonOrder.setCreateTime(DateUtils.getNowDate());
        int rows = zsyhPersonOrderMapper.insertZsyhPersonOrder(zsyhPersonOrder);
        insertZsyhPersonOrderMoney(zsyhPersonOrder);
        return rows;
    }

    /**
     * 修改个人扫码单
     * 
     * @param zsyhPersonOrder 个人扫码单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZsyhPersonOrder(ZsyhPersonOrder zsyhPersonOrder)
    {
        zsyhPersonOrder.setUpdateTime(DateUtils.getNowDate());
        zsyhPersonOrderMapper.deleteZsyhPersonOrderMoneyByOrderId(zsyhPersonOrder.getId());
        insertZsyhPersonOrderMoney(zsyhPersonOrder);
        return zsyhPersonOrderMapper.updateZsyhPersonOrder(zsyhPersonOrder);
    }


    public int updateZsyhPersonOrderApplet(ZsyhPersonOrder zsyhPersonOrder)
    {
        insertZsyhPersonOrderMoney(zsyhPersonOrder);
        return zsyhPersonOrderMapper.updateZsyhPersonOrder(zsyhPersonOrder);
    }

    /**
     * 删除个人扫码单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZsyhPersonOrderByIds(String ids)
    {
        zsyhPersonOrderMapper.deleteZsyhPersonOrderMoneyByOrderIds(Convert.toStrArray(ids));
        return zsyhPersonOrderMapper.deleteZsyhPersonOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除个人扫码单信息
     * 
     * @param id 个人扫码单ID
     * @return 结果
     */
    @Override
    public int deleteZsyhPersonOrderById(String id)
    {
        zsyhPersonOrderMapper.deleteZsyhPersonOrderMoneyByOrderId(id);
        return zsyhPersonOrderMapper.deleteZsyhPersonOrderById(id);
    }

    /**
     * 新增扫码单服务费信息
     * 
     * @param zsyhPersonOrder 个人扫码单对象
     */
    public void insertZsyhPersonOrderMoney(ZsyhPersonOrder zsyhPersonOrder)
    {
        List<ZsyhPersonOrderMoney> zsyhPersonOrderMoneyList = zsyhPersonOrder.getZsyhPersonOrderMoneyList();
        Long id = Long.valueOf(zsyhPersonOrder.getId());
        if (StringUtils.isNotNull(zsyhPersonOrderMoneyList))
        {
            List<ZsyhPersonOrderMoney> list = new ArrayList<ZsyhPersonOrderMoney>();
            for (ZsyhPersonOrderMoney zsyhPersonOrderMoney : zsyhPersonOrderMoneyList)
            {
                zsyhPersonOrderMoney.setOrderId(String.valueOf(id));
                list.add(zsyhPersonOrderMoney);
            }
            if (list.size() > 0)
            {
                zsyhPersonOrderMapper.batchZsyhPersonOrderMoney(list);
            }
        }
    }
}
